array-reduce-x
Reduce an array (from left to right) to a single value.
module.exports
⇒ *
⏏
This method applies a function against an accumulator and each element in the
array (from left to right) to reduce it to a single value.
Kind: Exported member
Returns: *
- The value that results from the reduction.
Throws:
TypeError
If array is null or undefined.TypeError
If callBack is not a function.TypeError
If called on an empty array without an initial value.
Param | Type | Description |
---|
array | array | The array to iterate over. |
callBack | function | Function to execute for each element. |
[initialValue] | * | Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce on an empty array without an initial value is an error. |
Example
import reduce from 'array-reduce-x';
console.log(
reduce(
[0, 1, 2, 3],
function(a, b) {
return a + b;
},
0,
),
);